package com.ecube.sql;

import com.ecube.mqtt.mysql.Query;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class Sql1580 {
    static Connection con;
    static Statement sql;
    static int res;
    public static void main(String[] args) {
        List list =new ArrayList();
        list.add("20250715");
        list.add("20250716");
        list.add("20250717");
        list.add("20250718");
        list.add("20250719");
        String F5 = "HDZ20241107001";
        String F6 = "154967";
        String F7 = "龙泉";
        for (int i = 0; i < list.size(); i++) {
            executeSql(list.get(i).toString(), F5, F6, F7);
        }
    }

    public static void executeSql(String F2,
                                  String F5,
                                  String F6,
                                  String F7) {
        for (int i = 0; i < 24; i++) {

            String F3 = "";
            String F4 = "";
            for (int j = 0; j < 4; j++) {
                if (j == 0) {
                    F4 = "00";
                }else if (j == 1) {
                    F4 = "15";
                }else if (j == 2) {
                    F4 = "30";
                }else if (j == 3) {
                    F4 = "45";
                }
                if (i<10) {
                    F3 = "0" + i;
                }else {
                    F3 = String.valueOf(i);
                }
                Date date = new Date();
                String F1 = F2  + F3 +  F4;
                SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyyMMdd");
                String F10 = dateFormat1.format(date);
                String ct = F10;
                Random random = new Random();
                // 生成一个随机数，范围在0（包含）到1000（不包含）之间
                double minimum = 200;
                double maximum = 400;
                double randomNumber = minimum + (double) (Math.random() * maximum);
                // 格式化为两位小数
                String F8 = String.format("%.2f", randomNumber);
                double minimum1 = 400;
                double maximum1 = 500;
                double randomNumber1 = minimum1 + (double) (Math.random() * maximum1);
                // 格式化为两位小数
                String F9 = String.format("%.2f", randomNumber1);
                getChargeSnno("UAT", F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, ct);
                System.out.println(F7 + "站" + F1 + "时段写入成功");
            }

        }
    }
    public static void getChargeSnno(String env,
                                       String F1,
                                       String F2,
                                       String F3,
                                       String F4,
                                       String F5,
                                       String F6,
                                       String F7,
                                       String F8,
                                       String F9,
                                       String F10,
                                       String ct) {
        Query c = new Query();
        con = c.getConnection(env, "cwp_ads");
        try {
            sql = con.createStatement();        // 实例化Statement对象
            String sqlexe = "INSERT INTO ads_zk1580\n" +
                    "(F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, ct)\n" +
                    "VALUES('" + F1 + "', '" + F2 + "', '" + F3 + "', '" + F4 + "', '" + F5 + "', '" + F6 + "', '" + F7 + "', '" + F8 + "', '" + F9 + "', '" + F10 + "', '" + ct + "');";
            System.out.println(sqlexe);
            res = sql.executeUpdate(sqlexe);        // 执行SQL语句，返回结果集
            System.out.println("执行" + res + "条成功");
//            while (res.next()) {
//                snno = res.getString("snno");
//                System.out.print(" snno:" + snno);
//            }
            //System.out.println(sqlexe);
            con.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public Connection getConnection(String env, String lib) {
        try {
            // 加载数据库驱动类
            Class.forName("com.mysql.jdbc.Driver");
            //Class.forName("com.mysql.cj.jdbc.Driver");
            // 连接数据库
            // 注意此处因为数据库和系统时区差异会引起异常所以在在jdbc连接的url后面加上
            String url = "";
            if (env.equals("UAT")) {
                url = "jdbc:mysql://pc-uf6j29qa2ez19opwm.rwlb.rds.aliyuncs.com:3306/" + lib;
                con = DriverManager.getConnection(url
                        + "?serverTimezone=UTC&characterEncoding=utf-8", "rhdk", "Rhdk@2019");
            } else if (env.equals("FAT")) {
                url = "jdbc:mysql://rm-uf6kl4uuc01g3018i123930.mysql.rds.aliyuncs.com:3306/" + lib;
                con = DriverManager.getConnection(url
                        + "?serverTimezone=UTC&characterEncoding=utf-8", "rhdk", "Rhdk@2019");
            }else if (env.equals("DEV")) {
                url = "jdbc:mysql://10.6.6.73:3306/" + lib;
                con = DriverManager.getConnection(url
                        + "?useSSL=false&serverTimezone=UTC&characterEncoding=utf-8", "root", "rhzl@2014");

                System.out.println(url);
            }


        } catch (Exception e) {
            System.out.println("连接数据库失败");
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
        return con;
    }
}
